package com.situ.stl.controller;

import com.situ.stl.constants.Constants;
import com.situ.stl.entity.Admin;
import com.situ.stl.service.AdminService;
import com.situ.stl.util.JWTUtil;
import com.situ.stl.util.Result;
import com.situ.stl.vo.AdminVo;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {

    @Autowired
    private AdminService adminService;
    @GetMapping
    public Result getAll(Integer page, Integer limit, Admin admin) {
        if (ObjectUtils.isEmpty(page) && ObjectUtils.isEmpty(limit)) {
            List<Admin> adminList = adminService.select(admin);
            return Result.success(Constants.STATUS_200, adminList);
        }
        return Result.success(Constants.STATUS_200, adminService.searchByPage(page, limit, admin));
    }
 @PutMapping
    public Result modifyAdmin(@RequestBody Admin admin){
     System.out.println("admin = " + admin);
     try {
         int row = adminService.modifyAdmin(admin);
         if (row == 0){
             return Result.error(Constants.EDIT_ADMIN_ERROR,"修改管理员失败");
         } else if (row == Constants.PHONE_HAS_ERROR) {
             return Result.error(Constants.PHONE_HAS_ERROR, "手机号被占用");
         }else if (row == Constants.PASSWORD_NULL_ERROR){
             return Result.error(Constants.PASSWORD_NULL_ERROR,"密码为空");
         } else if (row == Constants.PASSWORD_ERROR){
             return Result.error(Constants.PASSWORD_ERROR,"密码错误");
         }
     }catch (Exception e){
         log.info(e.getMessage());
         return Result.error(-1,e.getMessage());
     }

     return Result.success(Constants.STATUS_200);

    }
    @DeleteMapping("/{id}")
    public Result deleteAdmin(@PathVariable Integer id) {
        int row = adminService.deleteAdmin(id);
        if(row > 0) {
            return Result.success(Constants.STATUS_200);
        } else {
            return Result.success(Constants.STATUS_10001,"不能删除主管理员");
        }
    }
    @PostMapping
    public Result add(@RequestBody Admin admin){
        try {
            int row = adminService.addAdmin(admin);
            if (row == 0) {
                return Result.success(Constants.STATUS_10001, "添加管理员失败");
            } else if (row == Constants.PHONE_HAS_ERROR) {
                return Result.success(Constants.PHONE_HAS_ERROR, "手机号被占用");
            }
        }catch (Exception e){
           log.info("添加失败");
            return Result.success(Constants.STATUS_10001,"添加管理员失败");
        }

        return Result.success(Constants.STATUS_200);
    }
    @RequestMapping("/getAdmin")
    public void getAdmin(String phone) {
        System.out.println("传入的参数是:" + phone);
        Admin admin = adminService.getAdminByPhone(phone);
        System.out.println(admin);
    }
    @PostMapping("/login")
    public Result login(@RequestBody AdminVo adminVo) throws Exception {
        try {
           if (!StringUtils.hasText(adminVo.getKey())){
               throw new Exception("无效的验证码");
           }
            Claims claims = JWTUtil.parseJWT(adminVo.getKey());
            String oldCaptcha = claims.getSubject();
            if (!StringUtils.hasText(oldCaptcha)){
                return Result.error(Constants.CAPTCHA_OBS_ERROR,"验证码过期");
            }
            if (!oldCaptcha.equalsIgnoreCase(adminVo.getCaptcha())){
                return Result.error(Constants.CAPTCHA_ERROR,"验证码错误");
            }
           if (adminService.edge(adminVo) != 200){
               return Result.error(Constants.PASSWORD_ERROR,"密码错误");
           }
        }catch (Exception e){
            log.info("登录失败");
            return Result.success(Constants.STATUS_10001,e.getMessage());
        }
        // 校验登录
        Admin admin = adminService.login(adminVo);
        String token = JWTUtil.createJWT(admin.getPhone());
        return Result.success(Constants.STATUS_200, token);



    }
    @GetMapping("/getLogin")
    public Result getLogin(HttpServletRequest request) {
        // 获取Token
        String token = request.getHeader("token");
        if (StringUtils.hasText(token)) {
            try {
                // 解析Token
                Claims claim = JWTUtil.parseJWT(token);
                String phone = claim.getSubject();
                // 查询用户
                Admin admin = adminService.getByPhone(phone);

                if (!ObjectUtils.isEmpty(admin)) {
                    return Result.success(Constants.STATUS_200,admin);
                }
            } catch (Exception e) {
                log.info("token解析失败");
            }
        }

        return Result.error(Constants.STATUS_10001,"用户未登录！！");
    }
}
